package top.zhangjianyong.tools.service.etf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.constant.MaiRuiLevel;
import top.zhangjianyong.tools.entity.ETF;
import top.zhangjianyong.tools.entity.ETFData;
import top.zhangjianyong.tools.entity.marirui.JJETFHQ;
import top.zhangjianyong.tools.entity.marirui.JJLSKX;
import top.zhangjianyong.tools.exception.MairuiException;
import top.zhangjianyong.tools.service.mybatis.IETFService;
import top.zhangjianyong.tools.service.mybatis.IJJLSKXService;
import top.zhangjianyong.tools.utils.DateTimeUtil;
import top.zhangjianyong.tools.utils.HolidayUtil;
import top.zhangjianyong.tools.utils.MaiRuiApiUtil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/12/23 3:26 PM
 */
@Service
public class ETFMairuiServiceImpl implements ETFService {

    @Autowired
    private MaiRuiApiUtil maiRuiApiUtil;
    @Autowired
    private IETFService ietfService;
    @Autowired
    private HolidayUtil holidayUtil;
    @Autowired
    private IJJLSKXService ijjlskxService;

    @Override
    public ETF getETF(String code) {
        if (code.startsWith("sh") || code.startsWith("sz")) {
            JJETFHQ jjetfhq = maiRuiApiUtil.JJETFHQ(code);
            if (null == jjetfhq) {
                throw new MairuiException("查询基金失败");
            }
            return createETF(jjetfhq);
        } else {
            try {
                return getETF("sh" + code);
            } catch (MairuiException e) {
                return getETF("sz" + code);
            }
        }
    }

    private ETF createETF(JJETFHQ jjetfhq) {
        ETF etf = new ETF();
        etf.setCode(jjetfhq.getDm());
        etf.setName(jjetfhq.getMc());
        etf.setValue(new BigDecimal(jjetfhq.getZxj()));
        etf.setTotalValue(new BigDecimal(jjetfhq.getZxj()));
        etf.setDailyRate(new BigDecimal(jjetfhq.getZdf()));

        LocalTime time = DateTimeUtil.parseTime(jjetfhq.getT());
        LocalDate nowDate = LocalDate.now();
        if (LocalTime.now().isBefore(time)) {
            LocalDate date = holidayUtil.lastStock(nowDate);
            etf.setValueDate(LocalDateTime.of(date, time));
        } else {
            etf.setValueDate(LocalDateTime.of(nowDate, time));
        }
        return etf;
    }

    @Override
    public ETFData getETFDate(String code, int time) {
        ETF etf = getETF(code);
        return new ETFData(etf);
    }

    @Override
    public List<ETFData> getRecords(String code) {
        ETF etf = ietfService.getByCode(code);
        List<JJLSKX> jjlskx = maiRuiApiUtil.JJLSKX(code, MaiRuiLevel.DAY);
        // 入库jjlskx
        ijjlskxService.saveOrUpdate(jjlskx, code);
        return jjlskx.stream().map(kx -> new ETFData(kx, etf)).collect(Collectors.toList());
    }

    @Override
    public List<ETFData> getRecords(int page, String code) {
        return getRecords(code);
    }
}
